<?php
// 开启会话
session_start();

// 初始化电视台数据
if (!isset($_SESSION['tv_station'])) {
    $_SESSION['tv_station'] = [
        'funds' => 10000, // 初始资金
        'programs' => [], // 节目列表
        'ad_spots' => 5, // 广告位数量
        'viewership' => 500 // 观众数量
    ];
}

// 节目制作成本和收益
$program_costs = [
    'drama' => 2000,
    'news' => 1500,
    'variety' => 2500
];
$program_income = [
    'drama' => 3000,
    'news' => 2000,
    'variety' => 3500
];

// 广告销售收益
$ad_income = 1000;

// 处理节目制作操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['produce_program'])) {
    $program_type = $_POST['program_type'];
    $cost = $program_costs[$program_type];
    if ($_SESSION['tv_station']['funds'] >= $cost) {
        $_SESSION['tv_station']['funds'] -= $cost;
        $_SESSION['tv_station']['programs'][] = $program_type;
        // 简单模拟节目带来的观众增长和收入
        $_SESSION['tv_station']['viewership'] += rand(100, 300);
        $_SESSION['tv_station']['funds'] += $program_income[$program_type];
        $message = "成功制作 {$program_type} 节目，消耗 {$cost} 元，增加观众约 ". ($_SESSION['tv_station']['viewership'] - 500). " 人，收入 {$program_income[$program_type]} 元。";
    } else {
        $message = "资金不足，无法制作 {$program_type} 节目。";
    }
}

// 处理广告销售操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['sell_ad'])) {
    if ($_SESSION['tv_station']['ad_spots'] > 0) {
        $_SESSION['tv_station']['ad_spots']--;
        $_SESSION['tv_station']['funds'] += $ad_income;
        $message = "成功销售一个广告位，收入 {$ad_income} 元。";
    } else {
        $message = "没有可用的广告位，无法销售。";
    }
}

// 处理观众反馈操作（简单模拟，增加观众好感度，可影响后续收入）
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['handle_feedback'])) {
    $_SESSION['tv_station']['viewership'] += rand(50, 150);
    $message = "成功处理观众反馈，增加观众约 ". ($_SESSION['tv_station']['viewership'] - 500). " 人。";
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟电视台台长游戏</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }

        .info-list {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
    </style>
</head>

<body>
    <h1>模拟电视台台长游戏</h1>
    <div class="info-list">
        <p>当前资金: <?php echo $_SESSION['tv_station']['funds']; ?> 元</p>
        <p>节目列表: <?php echo implode(', ', $_SESSION['tv_station']['programs']) ?: '暂无节目'; ?></p>
        <p>可用广告位: <?php echo $_SESSION['tv_station']['ad_spots']; ?> 个</p>
        <p>观众数量: <?php echo $_SESSION['tv_station']['viewership']; ?> 人</p>
    </div>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '成功')!== false? 'success' : 'error'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="post">
        <label for="program_type">选择要制作的节目类型：</label>
        <select id="program_type" name="program_type">
            <option value="drama">电视剧</option>
            <option value="news">新闻节目</option>
            <option value="variety">综艺节目</option>
        </select>
        <button type="submit" name="produce_program">制作节目</button>
    </form>

    <form method="post">
        <button type="submit" name="sell_ad">销售广告位</button>
    </form>

    <form method="post">
        <button type="submit" name="handle_feedback">处理观众反馈</button>
    </form>
</body>

</html>